package com.dragee.modules.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.modules.work.dao.ShopDao;
import com.dragee.modules.work.entity.ShopEntity;
import com.dragee.modules.work.entity.ShopGoodsEntity;
import com.dragee.modules.work.entity.ShopProjectEntity;
import com.dragee.modules.work.po.UserGoodsPo;
import com.dragee.modules.work.service.ShopGoodsService;
import com.dragee.modules.work.service.ShopProjectGoodsService;
import com.dragee.modules.work.service.ShopProjectService;
import com.dragee.modules.work.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("shopService")
public class ShopServiceImpl extends ServiceImpl<ShopDao, ShopEntity> implements ShopService {


    @Resource
    private ShopDao shopDao;

    private String business = "shop";

    @Autowired
    private ShopProjectService shopProjectService;

    @Autowired
    private ShopGoodsService shopGoodsService;

    @Autowired
    private ShopProjectGoodsService shopProjectGoodsService;

    @Value("${server.port}")
    private String port;

    @Override
    public ShopEntity getShopByUserId(String userId) {
        ShopEntity shopEntity = getOne(new QueryWrapper<ShopEntity>()
                .eq("personId", userId), false);
        return shopEntity;
    }

    @Override
    public List<ShopProjectEntity> getProjects(String shopId) {
        List<ShopProjectEntity> projects = shopProjectService.list(new QueryWrapper<ShopProjectEntity>()
                .eq("shopId", shopId).orderByAsc("sort"));
        for (ShopProjectEntity p : projects) {
            List<UserGoodsPo> relationGoods = shopDao.getRelationGoods(p.getId().toString());
            p.setGoods(relationGoods);
        }
        return projects;
    }

    @Override
    public ShopProjectEntity getProjectById(String id) {
        ShopProjectEntity p = shopProjectService.getById(id);
        List<UserGoodsPo> relationGoods = shopDao.getRelationGoods(p.getId().toString());
        p.setGoods(relationGoods);
        return p;
    }

    @Override
    public List<ShopGoodsEntity> getGoods(String shopId) {
        List<ShopGoodsEntity> goods = shopGoodsService.list(new QueryWrapper<ShopGoodsEntity>()
                .eq("shopId", shopId));
        return goods;
    }
}